app.controller('BusPaymentListCtrl', function($scope, ajax, $modal, toaster) {

    //支付方式
    $scope.paymentList = [
        { id: 1, name: "支付宝" },
        { id: 2, name: "微信" },
        { id: 3, name: "微信公众号" },
    ];

    $scope.summary = [];
    $scope.doSummary = function() {
        ajax.call(new Req.GET({
            params: $scope.queryMap,
            resource: 'payment/3/summary'
        })).then(function(result) {
            $scope.summary = result.result;
            $scope.totalCount = $scope.summary[0]; //支付流水数
            $scope.charge = $scope.summary[1]; //应付金额
            $scope.pay = $scope.summary[2]; //实付金额
            $scope.insurances = $scope.summary[3]; //保险金额
            $scope.tickets = $scope.summary[4]; //票数
            $scope.flag = true;
        }, function() { });
    };

    $scope.reload = $scope.page.loader('payment/3/{pageIndex}/{pageSize}/page');
    $scope.$on('pageIndex', function(event, p) {
        $scope.reload(p, $scope.queryMap);
        $scope.doSummary();
    });
    $scope.reload(1);
    $scope.doSummary();

    $scope.queryMap = {};
    $scope.timeRange = {};
    $scope.query = function() {
        if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
            var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
            var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
            $scope.queryMap.payTime = starTime + "#@" + endTime;
        } else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
            delete $scope.queryMap.payTime;
        }
        if ($scope.paymentList.selected != undefined) {
            $scope.queryMap.payment__id = $scope.paymentList.selected.id;
        } else if ($scope.paymentList.selected == undefined) {
            delete $scope.queryMap.payment__id;
        }
        $scope.reload(1, $scope.queryMap);
        $scope.doSummary();
    };

    //导出Excel
    $scope.exportExcel = function() {
        if ($scope.paymentList.selected != undefined) {
            $scope.queryMap.payment__id = $scope.paymentList.selected.id;
        } else if ($scope.paymentList.selected == undefined) {
            delete $scope.queryMap.payment__id;
        }
        if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
            var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
            var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
            $scope.queryMap.payTime = starTime + "#@" + endTime;
        } else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
            delete $scope.queryMap.payTime;
        }
        ajax.call(new Req.POST({
            fields: $scope.queryMap,
            resource: 'payment/bs/export'
        })).then(function(result) {
            if (result.code == 200) {
                window.open(result.result);
                toaster.pop('success', '成功', '导出信息成功。');
            } else {
                toaster.pop('error', '错误', '导出信息失败！！');
            }
        }, function() { toaster.pop('error', '错误', '导出信息失败！'); });
    };

});
